package com.ruoyi.iot.modbus.tcp;


import com.serotonin.modbus4j.ModbusMaster;
import com.serotonin.modbus4j.exception.ErrorResponseException;
import com.serotonin.modbus4j.exception.ModbusTransportException;
import com.serotonin.modbus4j.locator.BaseLocator;
import com.serotonin.modbus4j.msg.*;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;


/**
 * @Description: modbus4j写入数据
 * @Author: Howe
 * @Date: 2019/11/28 11:27
 */
public class Modbus4jWriteUtils {
    static Log log = LogFactory.getLog(Modbus4jWriteUtils.class);


    /**
     * @Description: 写 [01 Coil Status(0x)]写一个 function ID = 5
     * @Param: [master, slaveId, writeOffset, writeValue]
     * @Return: boolean
     * @Author: Howe
     * @Date: 2019/11/28 11:27
     */
    public static boolean writeCoil(ModbusMaster master, int slaveId, int writeOffset, boolean writeValue)
            throws ModbusTransportException {

        // 创建请求
        WriteCoilRequest request = new WriteCoilRequest(slaveId, writeOffset, writeValue);
        // 发送请求并获取响应对象
        WriteCoilResponse response = (WriteCoilResponse) master.send(request);
        if (response.isException()) {
            return false;
        } else {
            return true;
        }
    }


    /**
     * @Description: 写[01 Coil Status(0x)] 写多个 function ID = 15
     * @Param: [master, slaveId, startOffset, bdata]
     * @Return: boolean
     * @Author: Howe
     * @Date: 2019/11/28 11:28
     */
    public static boolean writeCoils(ModbusMaster master, int slaveId, int startOffset, boolean[] bdata)
            throws ModbusTransportException {
        // 创建请求
        WriteCoilsRequest request = new WriteCoilsRequest(slaveId, startOffset, bdata);
        // 发送请求并获取响应对象
        WriteCoilsResponse response = (WriteCoilsResponse) master.send(request);
        if (response.isException()) {
            return false;
        } else {
            return true;
        }

    }


    /**
     * @Description: 写[03 Holding Register(4x)] 写一个 function ID = 6
     * @Param: [master, slaveId, writeOffset, writeValue]
     * @Return: boolean
     * @Author: Howe
     * @Date: 2019/11/28 11:28
     */
    public static boolean writeRegister(ModbusMaster master, int slaveId, int writeOffset, short writeValue)
            throws ModbusTransportException {
        // 创建请求对象
        WriteRegisterRequest request = new WriteRegisterRequest(slaveId, writeOffset, writeValue);
        WriteRegisterResponse response = (WriteRegisterResponse) master.send(request);
        if (response.isException()) {
            log.error(response.getExceptionMessage());
            return false;
        } else {
            return true;
        }

    }


    /**
     * @Description: 写入[03 Holding Register(4x)]写多个 function ID=16
     * @Param: [master, slaveId, startOffset, sdata]
     * @Return: boolean
     * @Author: Howe
     * @Date: 2019/11/28 11:28
     */
    public static boolean writeRegisters(ModbusMaster master, int slaveId, int startOffset, short[] sdata)
            throws ModbusTransportException {

        // 创建请求对象
        WriteRegistersRequest request = new WriteRegistersRequest(slaveId, startOffset, sdata);
        // 发送请求并获取响应对象
        ModbusResponse response = master.send(request);
        if (response.isException()) {
            log.error(response.getExceptionMessage());
            return false;
        } else {
            return true;
        }
    }


    /**
     * @Description: 写入数字类型的模拟量（如:写入Float类型的模拟量、Double类型模拟量、整数类型Short、Integer、Long）
     * @Param: [master, slaveId, offset, value, dataType]
     * @Return: void
     * @Author: Howe
     * @Date: 2019/11/28 11:28
     */
    public static void writeHoldingRegister(ModbusMaster master, int slaveId, int offset, Number value, int dataType)
            throws ModbusTransportException, ErrorResponseException {

        // 类型
        BaseLocator<Number> locator = BaseLocator.holdingRegister(slaveId, offset, dataType);
        master.setValue(locator, value);
    }

    public static void main(String[] args) {

    }

}